package net.dominatingapps.readcalendar;

import java.util.Locale;
import java.util.Random;
import java.util.HashSet;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
	
	private static final String TAG = "ReadCalendar";
	
	private TextToSpeech mTts;
	private Button mAgainButton;
	private static HashSet<String> events;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTts = new TextToSpeech(this,
                this  // TextToSpeech.OnInitListener
                );
        
        events = ReadCalendar.readCalendar(this);
 
        mAgainButton = (Button) findViewById(R.id.button1);

        mAgainButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sayEvents();
            }
        });
    }
    
    @Override
    public void onDestroy() {
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }

        super.onDestroy();
    }
    
    public void onInit(int status) {
        
        if (status == TextToSpeech.SUCCESS) {
           
            int result = mTts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e(TAG, "Language is not available.");
            } else {
               mAgainButton.setEnabled(true);
            }
        } else {
            Log.e(TAG, "Could not initialize TextToSpeech.");
        }
    }
    
    private void sayEvents() {
               
    	mTts.speak("Hello.  This is a reminder.", TextToSpeech.QUEUE_FLUSH, null);
    	
        for(String event : events) {
       
	        System.out.println(event);
	        
	        mTts.speak(event,
	            TextToSpeech.QUEUE_ADD,
	            null);
	        }
    }
}